package com.eqxiu.ieditor.api.impl.order;


import java.util.Objects;
import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eqxiu.ieditor.api.order.IOrderItemServiceApi;
import com.eqxiu.ieditor.dao.OrderItemMapper;
import com.eqxiu.ieditor.dto.ItemDTO;
import com.eqxiu.ieditor.dto.OrderDTO;
import com.eqxiu.ieditor.entity.OrderItem;

@Component
public class OrderItemServiceApiImpl implements IOrderItemServiceApi {
	@Autowired
	private OrderItemMapper orderItemMapper;

	@Override
	public void createItem(ItemDTO order) {
		OrderItem item = new OrderItem();
		BeanUtils.copyProperties(order, item);
		orderItemMapper.insert(item);
	}

	@Override
	public Optional<ItemDTO> selectByPrimaryKey(Integer id) {
		OrderItem order = orderItemMapper.selectByPrimaryKey(id);
		if (Objects.nonNull(order)) {
			ItemDTO dto = new ItemDTO();
			BeanUtils.copyProperties(order, dto);
			return Optional.of(dto);
		}
		return Optional.empty();
	}

	@Override
	public int countByItemType(String userId,String code, Integer itemType) {
		return orderItemMapper.countByItemType(userId, code, itemType);
	}

	@Override
	public void createOrderItem(OrderDTO dto) {
		OrderItem order = new OrderItem();
		BeanUtils.copyProperties(dto, order);
		this.orderItemMapper.insert(order);

	}

	@Override
	public OrderDTO getOrderItemById(Integer orderId) {
		OrderItem order = this.orderItemMapper.selectByPrimaryKey(orderId);
		OrderDTO orderDTO = new OrderDTO();
		BeanUtils.copyProperties(order, orderDTO);
		return orderDTO;
	}

}
